package com.hj.hos.config;

import cn.hutool.core.text.CharPool;
import com.hj.hos.enums.HosTypeEnum;
import com.hj.hos.properties.HosLocalProperties;
import com.hj.hos.properties.HosProperties;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author hj
 * @Date 2023/4/29
 * @Description: 本地资源映射配置
 */
@Configuration
@ConditionalOnProperty(prefix = "hos", value = "enabled")
public class LocalResourceConfiguration implements WebMvcConfigurer {

    @Resource
    private HosProperties properties;

    @Override
    public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
        // 如果不是本地存储，则返回
        if (properties.getConfig().getType() != HosTypeEnum.LOCAL) {
            return;
        }

        HosLocalProperties local = properties.getLocal();
        registry.addResourceHandler(CharPool.SLASH+ local.getUrl() + "/**")
                .addResourceLocations("file:" + local.getPath() + CharPool.SLASH);
    }


}

/*
*  org.springframework.beans.factory.annotation.Value;
*  这个绑定属性 @Value("${hj.hj}") yml小写. 配置类用大写不行.
*  yml大写,配置类用小写可以映射.....
*
* org.springframework.boot.context.properties.ConfigurationProperties
* @ConfigurationProperties(prefix = "hj") prefix必须小写,否则直接报错
* 然后 其他属性对应都不区分大小写.松散绑定.外面的前缀hj都可以写成 HJ hJ...
*
* */